package com.googlecode.jot.spring;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.config.AbstractFactoryBean;

import com.googlecode.jot.TranslationContext;
import com.googlecode.jot.TypeTranslator;

/**
 * {@link FactoryBean} for creating {@link TranslationContext}s.
 */
public class TranslationContextFactoryBean
	extends AbstractFactoryBean<TranslationContext> {

	private boolean fastNullConversions = true;
	private List<TypeTranslator<?, ?>> typeTranslators = new ArrayList<TypeTranslator<?, ?>>();

	/**
	 * {@inheritDoc}
	 */
	@Override
	public Class<TranslationContext> getObjectType() {
		return TranslationContext.class;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected TranslationContext createInstance()
		throws Exception {
		TranslationContext ctx = new TranslationContext();
		ctx.setFastNullConversions(fastNullConversions);
		for (TypeTranslator<?, ?> translator : typeTranslators) {
			ctx.registerTranslator(translator);
		}
		return ctx;
	}

	/**
	 * @param fastNullConversions the fastNullConversions to set
	 */
	public void setFastNullConversions(boolean fastNullConversions) {
		this.fastNullConversions = fastNullConversions;
	}

	/**
	 * @param typeTranslators the typeTranslators to set
	 */
	public void setTypeTranslators(List<TypeTranslator<?, ?>> typeTranslators) {
		this.typeTranslators = typeTranslators;
	}

}
